package com.itheima.controller.system;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.utils.BeanUtil;
import com.itheima.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/system/dept")
public class DeptServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if ("list".equals(operation)){
            this.list(request, response);
        }else if ("toAdd".equals(operation)){
            this.toAdd(request, response);
        }else if ("save".equals(operation)){
            this.save(request, response);
        }else if ("toEdit".equals(operation)){
            this.toEdit(request, response);
        }else if ("edit".equals(operation)){
            this.edit(request, response);
        }else if ("delete".equals(operation)){
            this.delete(request, response);
        }
    }

    // http://localhost:8080//system/dept?operation=list
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据
        //DeptService deptServlet=new DeptServiceImpl();
        int page=1;
        int size=5;
        if (StringUtils.isNotBlank(request.getParameter("page"))){
            page=Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))){
            size=Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = deptService.findAll(page,size);
        //存放数据
        request.setAttribute("page",all);
        //跳转list页面
        request.getRequestDispatcher("/WEB-INF/pages/system/dept/list.jsp").forward(request,response);
    }
    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //加载所有部门信息
        List<Dept> all = deptService.findAll();
        request.setAttribute("deptList",all);

        //跳转add页面
        request.getRequestDispatcher("/WEB-INF/pages/system/dept/add.jsp").forward(request,response);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应格式
        response.setContentType("text/html;charset=utf-8");
        String deptName = request.getParameter("deptName");
        String parentId = request.getParameter("parentId");
        String state = request.getParameter("state");
        System.out.println(deptName);
        System.out.println(parentId);
        System.out.println(state);


        //判断是否有值为空，有输出提示信息，在5秒后跳转回添加页面
//        if (dept.getDeptName().equals("")||dept.getParent().equals("")||dept.getState().equals(""))
        if (deptName.equals("")||parentId.equals("")||state.equals("")){
            String str="<font size=\"3\" color=\"red\">部门名不能为空或所属部门不能不选</font>";
            response.getWriter().write(str);
            response.setHeader("Refresh","5;URL=/system/dept?operation=toAdd");
        }else{
            //获取数据封装成对象
            Dept dept= BeanUtil.fillBean(request,Dept.class,"yyyy-MM-dd");
            //调用业务层接口save
            deptService.save(dept);
            //跳转回list页面
            response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
        }

    }
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        //查询数据
        //DeptService deptService=new DeptServiceImpl();

        Dept dept = deptService.findById(id);
        //加载所有部门信息
        List<Dept> all = deptService.findAll();
        request.setAttribute("deptList",all);

        request.setAttribute("dept",dept);
        //跳转update页面
        request.getRequestDispatcher("/WEB-INF/pages/system/dept/update.jsp").forward(request,response);
    }
    private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据封装成对象
        Dept dept= BeanUtil.fillBean(request,Dept.class,"yyyy-MM-dd");
        //调用业务层接口save
        //DeptService deptService=new DeptServiceImpl();
        deptService.update(dept);
        //跳转回list页面
        response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
    }
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据封装成对象
        Dept dept= BeanUtil.fillBean(request,Dept.class);
        //调用业务层接口save
        //DeptService deptService=new DeptServiceImpl();
        deptService.delete(dept);
        //跳转回list页面
        response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}